package com.css.fxfzfxqh.common.rest.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.constants.FxfzConstants;
import com.css.fxfzfxqh.modules.buildingKilometerGrid.exceptions.BizException;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import org.apache.commons.lang3.StringUtils;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Properties;

/**
 * @Description: fxfzfxqh
 * @Author: lhl
 * @CreateDate: 2023/12/15 16:00
 */
@RestController
@RequestMapping("/jcsjRest/")
public class JcsjRestController {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    SysAreaService sysAreaService;
    private static final String getGeomByProvinceCode = "datacenter/jcBoua2/getGeomByCode";
    private static final String getGeomByCityCode = "datacenter/jcBoua4/getGeomByCode";
    private static final String getGeomByCountyCode = "datacenter/jcBoua5/getGeomByCode";

    @GetMapping("getGeomByCode")
    public String getGeomByCode(@RequestParam Map<String, String> map) {
        try {
            Map<String, String> areaIdCodeMap = (Map<String, String>) JSON.parse((String) redisTemplate.opsForValue().get(FxfzConstants.CACHE_SYS_KEY + "areaIdCodeMap"));
            String province = map.get("provinceId");
            String city = map.get("cityId");
            String county = map.get("countyId");
            String type="3";
            String code=null;
            if (PlatformObjectUtils.isNotEmpty(province)){
                if (PlatformObjectUtils.isEmpty(city)){
                    type="1";
                    if (areaIdCodeMap==null||areaIdCodeMap.size()<1) {
                        code = sysAreaService.getDivisionCodeById(province);
                    }else {
                        code = areaIdCodeMap.get(province);
                    }
                }else {
                    if (PlatformObjectUtils.isEmpty(county)){
                        type="2";
                        if (areaIdCodeMap==null||areaIdCodeMap.size()<1) {
                            code = sysAreaService.getDivisionCodeById(city);
                        }else {
                            code = areaIdCodeMap.get(city);
                        }
                    }else {
                        if (areaIdCodeMap==null||areaIdCodeMap.size()<1) {
                            code = sysAreaService.getDivisionCodeById(county);
                        }else {
                            code = areaIdCodeMap.get(county);
                        }
                    }
                }
            }else {
                return null;
            }
            HttpEntity<String> entity = new HttpEntity<>(code, buildBaseHeaders());
            String url = null;
            try {
                if ("1".equals(type)) {
                    url = getBaseUrl(getGeomByProvinceCode);
                } else if ("2".equals(type)) {
                    url = getBaseUrl(getGeomByCityCode);
                } else {
                    url = getBaseUrl(getGeomByCountyCode);
                }
            } catch (BizException e) {
            return null;
            }
            JSONObject body = restTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class).getBody();
            if (body == null) {
                return null;
            }
            return (String) body.get("data");
        } catch (RestClientException e) {
            throw e;
        }
    }

    /**
     * 获取基础请求路径
     *
     * @return
     */
    private static String getBaseUrl(String apiUrl) throws BizException {
        Properties configGroup = PropsHelper.loadProps("slw-config.properties");
        String jcsjRestUrl = configGroup.getProperty("jcsjRestUrl");
        if (StringUtils.isBlank(jcsjRestUrl)) {
            throw new BizException("配置文件未发现基础数据系统接口URL");
        }
        if (!StringUtils.endsWith(jcsjRestUrl, "/")) {
            jcsjRestUrl += "/";
        }
        return jcsjRestUrl + apiUrl;
    }

    private static HttpHeaders buildBaseHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "dzzhfxpgyqhxt");
        headers.setContentType(MediaType.APPLICATION_JSON);
        return headers;
    }

}
